package com.zzdx.deployment.system.service.impl;

import com.zzdx.deployment.common.support.Convert;
import com.zzdx.deployment.system.domain.PrjversionConfig;
import com.zzdx.deployment.system.domain.Project;
import com.zzdx.deployment.system.domain.ProjectVersion;
import com.zzdx.deployment.system.mapper.ProjectVersionMapper;
import com.zzdx.deployment.system.service.IPrjversionConfigService;
import com.zzdx.deployment.system.service.IProjectService;
import com.zzdx.deployment.system.service.IProjectVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 项目版本 服务层实现
 * 
 * @author ruoyi
 * @date 2018-12-27
 */
@Service
public class ProjectVersionServiceImpl implements IProjectVersionService 
{
	@Autowired
	private ProjectVersionMapper projectVersionMapper;

	@Autowired
	private IProjectService projectService;

	@Autowired
	private IPrjversionConfigService prjversionConfigService;

	/**
     * 查询项目版本信息
     * 
     * @param id 项目版本ID
     * @return 项目版本信息
     */
    @Override
	public ProjectVersion selectProjectVersionById(Integer id)
	{
	    return projectVersionMapper.selectProjectVersionById(id);
	}
	
	/**
     * 查询项目版本列表
     * 
     * @param projectVersion 项目版本信息
     * @return 项目版本集合
     */
	@Override
	public List<ProjectVersion> selectProjectVersionList(ProjectVersion projectVersion)
	{
	    return projectVersionMapper.selectProjectVersionList(projectVersion);
	}

	@Override
	public List<ProjectVersion> selectProjectVersionListWithUserPerm(ProjectVersion projectVersion, String projectIds) {
		return projectVersionMapper.selectProjectVersionListWithUserPerm(projectVersion,Convert.toStrArray(projectIds));
	}

	/**
     * 新增项目版本
     * 
     * @param projectVersion 项目版本信息
     * @return 结果
     */
	@Override
	public int insertProjectVersion(ProjectVersion projectVersion)
	{
	    return projectVersionMapper.insertProjectVersion(projectVersion);
	}
	
	/**
     * 修改项目版本
     * 
     * @param projectVersion 项目版本信息
     * @return 结果
     */
	@Override
	public int updateProjectVersion(ProjectVersion projectVersion)
	{
	    return projectVersionMapper.updateProjectVersion(projectVersion);
	}

	/**
     * 删除项目版本对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteProjectVersionByIds(String ids)
	{
		//获取关联的全部配置
		List<PrjversionConfig> allPrjversionConfigs = new ArrayList<>();

		String[] pIds = Convert.toStrArray(ids);
		if(pIds.length > 0){
			for (String pId: pIds) {
				PrjversionConfig prjversionConfig = new PrjversionConfig();
				prjversionConfig.setProjectVersionId(Integer.parseInt(pId));
				List<PrjversionConfig> projectVersions = prjversionConfigService.selectPrjversionConfigList(prjversionConfig);
				allPrjversionConfigs.addAll(projectVersions);
			}
		}
		//先删除项目版本
		int updatedRows = projectVersionMapper.deleteProjectVersionByIds(Convert.toStrArray(ids));
		//再删除项目版本对应的配置
		if(updatedRows > 0 && allPrjversionConfigs.size()>0){
			StringBuilder idStrs = new StringBuilder();
			for (int i =0; i< allPrjversionConfigs.size();i++){
				PrjversionConfig prjversionConfig = allPrjversionConfigs.get(i);
				if(i == 0){
					idStrs.append(prjversionConfig.getId());
				}else{
					idStrs.append(","+prjversionConfig.getId());
				}
			}

			prjversionConfigService.deletePrjversionConfigByIds(idStrs.toString());
		}
		return updatedRows;
	}

	@Override
	public List<Map<String, Object>> selectProjectVersionTree(String projectIds) {
		List<ProjectVersion> projectVersions = projectVersionMapper.selectProjectVersionListWithUserPerm(new ProjectVersion(),Convert.toStrArray(projectIds));
		List<Map<String, Object>> trees = getTrees(projectVersions, false);
		return trees;
	}
	/**
	 * 对象转项目版本树
	 *
	 * @param projectVersionList 产品版本列表
	 * @param isCheck            是否需要选中
	 * @return
	 */
	public List<Map<String, Object>> getTrees(List<ProjectVersion> projectVersionList, boolean isCheck) {
		Set<String> projectIdSet = new HashSet<>();
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < projectVersionList.size(); i++) {
			ProjectVersion projectVersion = projectVersionList.get(i);
			Integer projectId = projectVersion.getProjectId();
			Project project = projectService.selectProjectById(projectId);
			//若项目ID未加入，则添加，否则路过
			if(!projectIdSet.contains(projectId.toString())){
				Map<String, Object> projectMap = new HashMap<String, Object>();
				projectMap.put("id", projectId);
				projectMap.put("pId", 0);
				projectMap.put("name", project.getName());
				projectMap.put("title", project.getName());
				projectIdSet.add(projectId.toString());
				trees.add(projectMap);
			}
			Map<String, Object> projectVersionMap = new HashMap<String, Object>();
			projectVersionMap.put("id", projectVersion.getId());
			projectVersionMap.put("pId", projectId);
			projectVersionMap.put("name", projectVersion.getVersion());
			projectVersionMap.put("title", projectVersion.getVersion());
			if (isCheck && i == 0) {
				projectVersionMap.put("checked", true);
			} else {
				projectVersionMap.put("checked", false);
			}

			trees.add(projectVersionMap);
		}
		return trees;
	}
}
